package base

import (
	"bytes"
	"fmt"
	"github.com/gin-gonic/gin"
	"html/template"
	"io"
	"net/http"
	"vitepress-serve/internal/common/hgin"
)

type Controller struct {
}

// HTML 渲染html页面
func (Controller) HTML(ctx *gin.Context, tpl *template.Template, name string, data any) {
	hgin.HTML(ctx, tpl, name, data)
}

// GetRequestBody 读请求Body
func (Controller) GetRequestBody(request *http.Request) ([]byte, error) {
	body, err := io.ReadAll(request.Body)
	if err != nil {
		return nil, fmt.Errorf("read request body err: %v", err)
	}
	_ = request.Body.Close()
	request.Body = io.NopCloser(bytes.NewBuffer(body))
	return body, nil
}
